// luch-request请求插件，类似axios
import Request from '@/utils/luch-request/index.js'
import baseUrl from '@/utils/baseUrl.js' // api接口地址
import tim from '@/utils/commen/tim.js'

import store from '@/store/index.js'

const http = new Request()

// 跳转到登录页面
const toLogin = msg => {
  setTimeout(function() {

    // 登录信息-清除vuex
    store.commit('setModel', null)
    //token存储-清除缓存
    uni.removeStorageSync('token')
    //邀请码存储-清除缓存
    uni.removeStorageSync('invitationcode')
    //用户信息-清除缓存
    uni.removeStorageSync('Ztc_C_USER_INFO')
    uni.setStorage({
      key: 'launchFlag',
      data: false,
    })
    //清除身份认证
    uni.removeStorageSync('idcards')

    store.commit('isSDKReady', false)
    tim.tim.logout().then(imResponse => {}).catch(imError => {})

    let logintrue = uni.getStorageSync('logintrue')

    if (logintrue) {
      // 跳转逻辑
      console.log('调取登录次数100')
      // uni.redirectTo({
      //   url: '/pages_logins/login/login'
      // })
      uni.navigateTo({
        url: '/pages_logins/login/login'
      })
      uni.removeStorageSync('logintrue')
    }
  }, 1000)
  uni.setStorage({
    key: 'launchFlag',
    data: false,
  })
}

// 消息提示框弹窗
const msgAlert = (msg, success = () => {}) => {
  uni.showToast({
    title: msg || '网络异常，请稍后重试',
    icon: 'none',
    duration: 2000,
    success: success,
  })
}

// 设置默认配置
http.setConfig(config => {
  config.baseURL = baseUrl // 接口地址
  config.timeout = 6000
  return config
})

http.interceptors.request.use(config => {
  config.header = {
    ...config.header,
    'content-type': 'application/json; charset=utf-8',
    // 'Auth': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJNb2JpbGUiOiIxMzUxMTExMTExOSIsIlVzZXJJZCI6ImZiYzZjZTUwNGU0NjRmYWE5NDgyMWViMmE1OTI1OGNiIiwiRXhwaXJlVGltZSI6IjIwMjEtMDQtMTNUMDk6MDY6NDAuNTQ1NjQ5OCswODowMCIsIkd1aWQiOiJjMDQ0N2ZkMGZkNzA0MjBmYmIxYmYwYzM4MDRhMTgyNSIsIkFwcF90eXBlIjowLCJSb2xlIjpudWxsLCJQZXJtaW91cyI6IiJ9.CK7TLmWo-Zoaqgd2ogLpbd07MhOXtH7h6u6QiKuelDY',
    'Auth': uni.getStorageSync('token'),
  }
  return config
})

// 响应拦截器
http.interceptors.response.use(
  // 成功响应
  res => {
    if (res.statusCode === 200) {
      switch (res.data.code) {
      case -2:
        msgAlert(res.data.msg)
        break
      case -3:
        msgAlert(res.data.msg)
        break
      case -4:
        if (res.config.url.indexOf('SetSailGetState') > -1 || res.config.url.indexOf('GetMyInProgressOrder') > -1 ||
            res.config.url.indexOf('GetCheckMyInProgressOrder') > -1) {

        } else {
          msgAlert(res.data.msg)
        }
        break
      case -7:
        msgAlert(res.data.msg, () => {
          console.log('调取登录次数1')
          uni.setStorageSync('logintrue', true)
          toLogin(res.data.msg)
        })
        break
      }
      return res.data
    } else {
      return Promise.reject(res.data)
    }
  },

  // 错误响应
  res => {
    // console.log(res)
    if (res.statusCode === 500) {
      switch (res.data.code) {
      case -1:
        msgAlert(res.data.msg, () => {
          // console.log('调取登录次数2')
          uni.setStorageSync('logintrue', true)
          toLogin(res.data.msg)
        })
        break
      case -2:
        msgAlert(res.data.msg)
        break
      case -3:
        msgAlert(res.data.msg)
        break
      case -4:
        if (res.config.url.indexOf('SetSailGetState') > -1 || res.config.url.indexOf('GetMyInProgressOrder') > -1) {

        } else {
          msgAlert(res.data.msg)
        }
        break
      case -5:
        msgAlert(res.data.msg, () => {
          // console.log('调取登录次数3')
          uni.setStorageSync('logintrue', true)
          toLogin(res.data.msg)
        })
        break
      case -6:
        let msg = res.data.msg + ', 请稍后'
        msgAlert(res.data.msg)
        break
      case 500:
        msgAlert('系统异常，请稍后重试或联系客服')
        break
      case 501:
        msgAlert('系统异常，请稍后重试或联系客服')
        break
      case 502:
        msgAlert(res.data.msg)
        break
      case 503:
        msgAlert(res.data.msg)
        break
      }
    } else {
      switch (res.statusCode) {
      case 401:
        msgAlert(res.data.msg, () => {
          // console.log('调取登录次数4')
          uni.setStorageSync('logintrue', true)
          toLogin(res.data.msg)
        })
        break
      case 403:
        msgAlert(res.data.msg)
        break
      case 404:
        msgAlert(res.data.msg)
        break
      case 501:
        msgAlert('系统异常，请稍后重试或联系客服')
        break
      case 502:
        msgAlert('系统异常，请稍后重试或联系客服')
        break
      }
    }
    return Promise.reject(res.data.msg)
  }
)

export default http
